﻿using System;
using System.Windows;
using AzureDiagMon.Common.Mvvm;
using AzureDiagMon.Common.Mvvm.Helpers;
using AzureDiagMon.Common.Mvvm.Interfaces;
using AzureDiagMon.Common.ViewModels;
using AzureDiagMon.Common.Views;

namespace AzureDiagMon.Common.Helpers
{
    public class InteractionService : IInteractionService
    {
        #region Implementation of IInteractionService

        public bool ShowDialog(object view, out IChildViewModel childViewModel, Action<IChildViewModel> initializeViewModel = null)
        {
            var viewElement = view as FrameworkElement;

            if (viewElement == null)
                throw new Exception(string.Format("Child ViewModel is not a FrameworkElement. Its type is \"{0}\"", view.GetType().Name));

            childViewModel = viewElement.DataContext as IChildViewModel;

            if (childViewModel == null)
                throw new Exception(string.Format("Child ViewModel not found for \"{0}\" view", view));

            if (initializeViewModel != null)
                initializeViewModel(childViewModel);

            var dialog = new ModalDialog();

            var hostViewModel = dialog.DataContext as ModalDialogViewModel;
            if (hostViewModel == null)
                throw new Exception("Initialization error");

            hostViewModel.ChildControl = viewElement;

            dialog.ShowDialog();

            if (dialog.DialogResult.HasValue)
                return dialog.DialogResult.Value;

            return false;
        }

        public bool ShowDialog(IChildViewModel childViewModel)
        {
            var dialog = new ModalDialog();

            var hostViewModel = dialog.DataContext as ModalDialogViewModel;
            if (hostViewModel == null)
                throw new Exception("Initialization error");

            var childControl = ViewModelHelper.GetView(childViewModel) as FrameworkElement;
            if (childControl == null)
                throw new Exception("View not found!");

            childControl.DataContext = childViewModel;
            hostViewModel.ChildControl = childControl;

            dialog.ShowDialog();

            if (dialog.DialogResult.HasValue)
                return dialog.DialogResult.Value;

            return false;
        }


        public void ShowMessage(string message, string title = null, MessageCategory category = MessageCategory.Information)
        {
            var button = MessageBoxButton.OK;
            var icon = MessageBoxImage.None;

            switch (category)
            {
                case MessageCategory.Unspecified:
                    break;
                case MessageCategory.Information:
                    icon = MessageBoxImage.Information;
                    break;
                case MessageCategory.Warning:
                    icon = MessageBoxImage.Warning;
                    break;
                case MessageCategory.Error:
                    icon = MessageBoxImage.Error;
                    break;
                case MessageCategory.Question:
                    icon = MessageBoxImage.Question;
                    break;
                default:
                    throw new ArgumentOutOfRangeException("category");
            }

            //TODO: AppTitle
            if (title == null)
                title = "";// Resources.AppTitle;

            MessageBox.Show(message, title, button, icon);
        }

        public ConfirmationResult ShowConfirmation(string message, string title = null, MessageCategory category = MessageCategory.Information, ConfirmationCategory confirmationCategory = ConfirmationCategory.OkCancel)
        {
            var button = MessageBoxButton.OKCancel;
            var icon = MessageBoxImage.None;

            switch (category)
            {
                case MessageCategory.Unspecified:
                    break;
                case MessageCategory.Information:
                    icon = MessageBoxImage.Information;
                    break;
                case MessageCategory.Warning:
                    icon = MessageBoxImage.Warning;
                    break;
                case MessageCategory.Error:
                    icon = MessageBoxImage.Error;
                    break;
                case MessageCategory.Question:
                    icon = MessageBoxImage.Question;
                    break;
                default:
                    throw new ArgumentOutOfRangeException("category");
            }

            switch (confirmationCategory)
            {
                case ConfirmationCategory.Unspecified:
                    break;
                case ConfirmationCategory.OkCancel:
                    break;
                case ConfirmationCategory.YesNo:
                    button = MessageBoxButton.YesNo;
                    break;
                case ConfirmationCategory.YesNoCancel:
                    button = MessageBoxButton.YesNoCancel;
                    break;
                default:
                    throw new ArgumentOutOfRangeException("confirmationCategory");
            }

            //TODO: AppTitle
            if (title == null)
                title = "";// Resources.AppTitle;

            var result = MessageBox.Show(message, title, button, icon);

            switch (result)
            {
                case MessageBoxResult.None:
                    return ConfirmationResult.Unknown;
                case MessageBoxResult.OK:
                    return ConfirmationResult.Ok;
                case MessageBoxResult.Cancel:
                    return ConfirmationResult.Cancel;
                case MessageBoxResult.Yes:
                    return ConfirmationResult.Yes;
                case MessageBoxResult.No:
                    return ConfirmationResult.No;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        //public bool ShowDialog(string viewName, out IChildViewModel childViewModel, Action<IChildViewModel> initializeViewModel = null)
        //{
        //    var dialog = new ModalDialog();

        //    var hostViewModel = dialog.DataContext as ModalDialogViewModel;
        //    if (hostViewModel == null)
        //        throw new Exception("Initialization error");

        //    childViewModel = ViewModelHelper.GetViewModel(viewName) as IChildViewModel;
        //    if (childViewModel == null)
        //        throw new Exception(string.Format("Child ViewModel not found for \"{0}\" view", viewName));

        //    if (initializeViewModel != null)
        //        initializeViewModel(childViewModel);

        //    hostViewModel.ChildViewModel = childViewModel;

        //    dialog.ShowDialog();

        //    if (dialog.DialogResult.HasValue)
        //        return dialog.DialogResult.Value;

        //    //TODO: Is it OK? Exception instead?
        //    return false;
        //}



        #endregion
    }
}
